//
//  SettingViewController.m
//  QuickCure1
//
//  Created by maoge on 16/10/30.
//  Copyright © 2016年 maoge. All rights reserved.
//


#import "SettingViewController.h"
#import "LXFeedBackViewController.h"
#import "LXModifyPasswordController.h"
#import "FeedBackViewController.h"
#import "LoginViewController.h"


@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *dataList;

@end

static NSString *settingCellID = @"settingCell";

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupUI];
    
    
    
}

- (void)setupUI{
    
    UITableView *tabelView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    [self.view addSubview:tabelView];
    
    [tabelView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    tabelView.separatorColor = [UIColor whiteColor];
    
    tabelView.dataSource = self;
    
    tabelView.delegate = self;
    
    [tabelView registerClass:[UITableViewCell class] forCellReuseIdentifier:settingCellID];
    
}

- (void)loadData{
    
    _dataList = @[@"意见反馈",@"加号符号说明",@"修改密码",@"注销用户"];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataList.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCellID forIndexPath:indexPath];
    
    cell.textLabel.text = _dataList[indexPath.row];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        
        
        FeedBackViewController *feedBackViewController = [[FeedBackViewController alloc] init];
        
        feedBackViewController.title = @"意见反馈";
        
        [self.navigationController pushViewController:feedBackViewController animated:YES];
        
    }
    
    if (indexPath.row == 3) {
        
        NSAssert(_logOutBlock != nil, @"logOutBlock不能为空！");
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"你确定需要注销吗" preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        if ([MLDefaults boolForKey:kIsUserLoginKey]) {
            
            UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                [MLDefaults setBool:NO forKey:kIsUserLoginKey];
                
                _logOutBlock();
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            }];
            [alert addAction:cancelAction];
            [alert addAction:logoutAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else {
            
             UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"对不起,您需要请登录" preferredStyle:UIAlertControllerStyleActionSheet];
            
             UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LoginViewController" bundle:nil];
                
                LoginViewController *vc = [sb instantiateInitialViewController];
                
                [self.navigationController pushViewController:vc animated:YES];
                
            }];
            
            [alert addAction:cancelAction];
            [alert addAction:logoutAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
       
}
    
    if (indexPath.row == 2) {
        
        if ([MLDefaults boolForKey:kIsUserLoginKey])
        {
            LXModifyPasswordController *modifyPasswordController = [[UIStoryboard storyboardWithName:@"LXModifyPasswordController" bundle:nil]instantiateInitialViewController];
            
            modifyPasswordController.user = _user;
            
            [self.navigationController pushViewController:modifyPasswordController animated:YES];
            
        }
        else
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您还没有登录，请登录!" preferredStyle:UIAlertControllerStyleActionSheet];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            UIAlertAction *logoutAction = [UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                UIStoryboard *sb = [UIStoryboard storyboardWithName:@"LoginViewController" bundle:nil];
                
                UIViewController *vc = [sb instantiateInitialViewController];
                
                [self.navigationController pushViewController:vc animated:YES];
                
            }];
            
            [alert addAction:cancelAction];
            [alert addAction:logoutAction];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    }
        
    if (indexPath.row == 1) {
        
        BaseViewController *viewController = [[BaseViewController alloc] init];
        
        UIWebView *webView = [[UIWebView alloc] init];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
        
        [webView loadRequest:request];
        
        [viewController.view addSubview:webView];
        
        webView.frame = viewController.view.frame;
        
        [self.navigationController pushViewController:viewController animated:YES];
    
    }
}





@end
